package com.helasasa.maxfunds.lineofcredit.cashloan.ghana.screen.splash

import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.helasasa.maxfunds.lineofcredit.cashloan.ProjectApplication
import com.helasasa.maxfunds.lineofcredit.cashloan.ghana.http.IApiProcessCallback
import com.helasasa.maxfunds.lineofcredit.cashloan.ghana.http.createApiAction
import com.helasasa.maxfunds.lineofcredit.cashloan.ghana.http.createZoneAction
import com.helasasa.maxfunds.lineofcredit.cashloan.ghana.http.doPostRequest
import com.helasasa.maxfunds.lineofcredit.cashloan.ghana.http.doZoneRequest
import com.helasasa.maxfunds.lineofcredit.cashloan.ghana.screen.tabhome.TabHomeGhanaActivity
import com.helasasa.maxfunds.lineofcredit.cashloan.ghana.user.mainProcessFormat
import org.json.JSONArray
import org.json.JSONObject

class SplashGhanaViewModel : ViewModel() {

    private val _zoneResult = MutableLiveData<SplashGhanaZoneResult>()
    val zoneResult: LiveData<SplashGhanaZoneResult> = _zoneResult

    private val _sysConfigResult = MutableLiveData<SplashGhanaSysConfigResult>()
    val sysConfigResult: LiveData<SplashGhanaSysConfigResult> = _sysConfigResult

    private val _mainProcessResult = MutableLiveData<SplashGhanaMainProcessResult>()
    val mainProcessResult: LiveData<SplashGhanaMainProcessResult> = _mainProcessResult

    private val _nextRouterResult =
        MutableLiveData<SplashGhanaNextRouterResult<AppCompatActivity>>()
    val nextRouterResult: LiveData<SplashGhanaNextRouterResult<AppCompatActivity>> =
        _nextRouterResult

    /** 查询时区接口 */
    fun zone() {
        val context = ProjectApplication.context
        val action = HashMap<String, Any>().createZoneAction()

        context.doZoneRequest(action, object : IApiProcessCallback {
            override fun onApiReady() {
            }

            override fun onApiDone(jsonObject: JSONObject) {
                val data = jsonObject.optString("data")
                val zoneFormat = data.split(",")
                if (zoneFormat.isNotEmpty()) {
                    if (zoneFormat.size > 1) {
                        ProjectApplication.mPhoneCode = "+" + zoneFormat[1]
                    }
                    ProjectApplication.zonePathAction(isGet = false, zoneFormat.first())
                    _zoneResult.value = SplashGhanaZoneResult(success = true)
                }
            }

            override fun onApiError(code: String, errMsg: String) {
                _zoneResult.value = SplashGhanaZoneResult(error = errMsg)
            }
        })

    }

    /**
     * 系统配置接口
     */
    fun sysConfig() {
        val context = ProjectApplication.context
        val action = hashMapOf<String, Any>().createApiAction()
        action["path"] = "Jdvrwau2zkjCCvwpptC8w"

        context.doPostRequest(action, object : IApiProcessCallback {
            override fun onApiReady() {
            }

            override fun onApiDone(jsonObject: JSONObject) {
                val obj = jsonObject.optJSONObject("obj") ?: JSONObject()
                val customize = obj.optJSONObject("customize")
                if (customize != null) {
                    // 更新客服
                    val mobile = customize.optString("mobile")
                    val email = customize.optString("email")
                    ProjectApplication.updateHotlines(mobiles = mobile, emails = email)

                    // 更新首页额度
                    var homeAmount = customize.optString("homeAmount")
                    if (homeAmount.isBlank()) {
                        homeAmount = "300000"
                        customize.put("homeAmount", homeAmount)
                    }
                    ProjectApplication.updateHomeAmount(homeAmount)

                    // 更新权限
                    val permissions = customize.optJSONObject("permissions") ?: JSONObject()
                    val splashPer = permissions.optJSONArray("permission") ?: JSONArray()
                    val applyPer = permissions.optJSONArray("home") ?: JSONArray()
                    ProjectApplication.updateSplashPermissions(splashPer)
                    ProjectApplication.updateApplyPermissions(applyPer)

                    // 更新运营商列表-法语
                    val walletChannels = customize.optJSONArray("walletChannels") ?: JSONArray()
                    ProjectApplication.updateWalletChannels(walletChannels)

                    // 更新测试账号列表-法语
                    val accList = customize.optJSONArray("accListV2") ?: JSONArray()
                    ProjectApplication.updateAccUsers(accList)

                    _sysConfigResult.value = SplashGhanaSysConfigResult(success = customize)
                } else {
                    _sysConfigResult.value = SplashGhanaSysConfigResult(success = JSONObject())
                }
            }

            override fun onApiError(code: String, errMsg: String) {
                _sysConfigResult.value =
                    SplashGhanaSysConfigResult(error = errMsg)
            }
        })

    }

    /** 查询主流程接口 */
    fun mainProcess() {
        val context = ProjectApplication.context
        val action = hashMapOf<String, Any>().createApiAction()
        action["path"] = "D50PrK3UELaosdHcVCL7A"

        context.doPostRequest(action, object : IApiProcessCallback {
            override fun onApiReady() {
            }

            override fun onApiDone(jsonObject: JSONObject) {
                val obj = jsonObject.optJSONObject("obj")
                _mainProcessResult.value = SplashGhanaMainProcessResult(success = obj)
            }

            override fun onApiError(code: String, errMsg: String) {
                _mainProcessResult.value = SplashGhanaMainProcessResult(error = errMsg)
            }
        })

    }

    /** 页面跳转 */
    fun nextRouter(jsonObject: JSONObject) {
        // 判断下一步
        val nextRouterResult = jsonObject.mainProcessFormat()
        if (nextRouterResult.nextRouter != null) {
            _nextRouterResult.value =
                SplashGhanaNextRouterResult(nextPage = nextRouterResult.nextRouter)
        }
    }

    /**
     * 查询用户状态
     */
    fun checkUserState() {
        // 判断登录
        if (ProjectApplication.onActionReady()) {
            mainProcess()
        } else {
            _nextRouterResult.value =
                SplashGhanaNextRouterResult(nextPage = TabHomeGhanaActivity::class.java)
        }
    }
}